﻿using _IMES.DBUtils;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using houseAgency.Base;
using houseAgency.Models;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;
using Type = houseAgency.Models.Type;

namespace houseAgency.ViewModel.SeekTenants
{
    public class HouseViewModel : ObservableObject
    {

        public HouseViewModel()
        {
            GetAllType();
            GetAllSeat();
            GetAllFloor();
            GetAllMothed();
            GetAllFavor();
            GetAllFitment();

            if (Global.selectedHouse.ID != null && Global.selectedHouse.ID != "")
            {
                House = Global.selectedHouse;
                OKButtonVisibility = Visibility.Collapsed;
                ButtonVisibility = Visibility.Visible;
            }
        }
        #region 属性
        private string showInfo;

        public string ShowInfo
        {
            get { return showInfo; }
            set
            {
                showInfo = value;
                OnPropertyChanged();
            }
        }

        private House house;

        public House House
        {
            get { return house; }
            set
            {
                house = value;
                OnPropertyChanged();
            }
        }

        private List<Type> allType = new();

        public List<Type> AllType
        {
            get { return allType; }
            set
            {
                allType = value;
                OnPropertyChanged();
            }
        }

        private List<Seat> allSeat = new List<Seat>();

        public List<Seat> AllSeat
        {
            get { return allSeat; }
            set
            {
                allSeat = value;
                OnPropertyChanged();
            }
        }

        private List<Floor> allFloor = new List<Floor>();

        public List<Floor> AllFloor
        {
            get { return allFloor; }
            set
            {
                allFloor = value;
                OnPropertyChanged();
            }
        }

        private List<Mothed> allMothed = new List<Mothed>();

        public List<Mothed> AllMothed
        {
            get { return allMothed; }
            set
            {
                allMothed = value;
                OnPropertyChanged();
            }
        }

        private List<Favor> allFavor = new List<Favor>();

        public List<Favor> AllFavor
        {
            get { return allFavor; }
            set
            {
                allFavor = value;
                OnPropertyChanged();
            }
        }

        private List<Fitment> allFitment = new List<Fitment>();

        public List<Fitment> AllFitment
        {
            get { return allFitment; }
            set
            {
                allFitment = value;
                OnPropertyChanged();
            }
        }

        private bool oKButtonEnable = true;

        public bool OKButtonEnable
        {
            get { return oKButtonEnable; }
            set
            {
                oKButtonEnable = value;
                OnPropertyChanged();
            }
        }
        private Visibility buttonVisibility = Visibility.Hidden;

        public Visibility ButtonVisibility
        {
            get { return buttonVisibility; }
            set
            {
                buttonVisibility = value;
                OnPropertyChanged();
            }
        }
        private Visibility oKButtonVisibility = Visibility.Visible;

        public Visibility OKButtonVisibility
        {
            get { return oKButtonVisibility; }
            set
            {
                oKButtonVisibility = value;
                OnPropertyChanged();
            }
        }
        #endregion

        #region 命令 Command
        public ICommand GetAllTypeCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (WindowManager.Show("TypeWindow", null) == false)
                        GetAllType();
                });
            }
        }

        public ICommand GetAllSeatCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (WindowManager.Show("SeatWindow", null) == false)
                        GetAllSeat();
                });
            }
        }
        public ICommand GetAllFloorCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (WindowManager.Show("FloorWindow", null) == false)
                        GetAllFloor();
                });
            }
        }
        public ICommand GetAllMothedCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (WindowManager.Show("MothedWindow", null) == false)
                        GetAllMothed();
                });
            }
        }
        public ICommand GetAllFavorCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (WindowManager.Show("FavorWindow", null) == false)
                        GetAllFavor();
                });
            }
        }
        public ICommand GetAllFitmentCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (WindowManager.Show("FitmentWindow", null) == false)
                        GetAllFitment();
                });
            }
        }
        public ICommand OKCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    House.ID = GetHouseId();
                    AddHouseInfo();
                });
            }
        }
        public ICommand ChangeCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (MessageBox.Show(String.Format("您确定要修改编号为：{0}的房屋信息吗？", House.ID), "修改操作", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                    {
                        bool result = UpdateHouseInfo(House);
                        if (result)
                        {
                            MessageBox.Show("修改成功");
                        }
                        else
                        {
                            MessageBox.Show("修改失败");
                        }
                    }
                });
            }
        }

        #endregion

        #region 方法
        private void GetAllType()
        {
            string sql = "select * from type";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.AllType = DBConvert.DataTableToList<Type>(dataTable);
            }
        }
        private void GetAllSeat()
        {
            string sql = "select * from seat";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.AllSeat = DBConvert.DataTableToList<Seat>(dataTable);
            }
        }
        private void GetAllFloor()
        {
            string sql = "select * from floor";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.AllFloor = DBConvert.DataTableToList<Floor>(dataTable);
            }
        }
        private void GetAllMothed()
        {
            string sql = "select * from mothed";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.AllMothed = DBConvert.DataTableToList<Mothed>(dataTable);
            }
        }
        private void GetAllFavor() 
        {
            string sql = "select * from favor";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.AllFavor = DBConvert.DataTableToList<Favor>(dataTable);
            }
        }
        private void GetAllFitment()
        {
            string sql = "select * from fitment";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.AllFitment = DBConvert.DataTableToList<Fitment>(dataTable);
            }
        }
        private string GetHouseId()
        {
            string id = "hou";
            string sql = "select MAX(id) from house";
            string maxId = MySQLHelper.ExecuteScalar<string>(MySQLHelper.DBConnectionString, sql);
            if (maxId != "")
            {
                id += (int.Parse(maxId.Substring(3)) + 1).ToString();
            }
            else
            {
                id += 1001;
            }
            return id;
        }
        private void AddHouseInfo()
        {
            string sql = "insert into house(id,companyName,tpyeID,seatID,state,favorID,fitmentID,mothedID,map,price,floorID,buildYear,area,remark,userID) values(@id,@companyName,@tpyeID,@seatID,@state,@favorID,@fitmentID,@mothedID,@map,@price,@floorID,@buildYear,@area,@remark,@userID)";
            int result = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] {
                new MySqlParameter("id",House.ID), new MySqlParameter("companyName",House.CompanyName), new MySqlParameter("typeID",House.TypeID), new MySqlParameter("seatID",House.SeatID), new MySqlParameter("state","none"), new MySqlParameter("favorID",House.FavorID),new MySqlParameter("fitmentID",House.FitmentID),
                    new MySqlParameter("mothedID",House.MothedID), new MySqlParameter("map",""), new MySqlParameter("price",House.Price), new MySqlParameter("floorID",House.FloorID), new MySqlParameter("buildYear",House.BuildYear), new MySqlParameter("area",House.Area), new MySqlParameter("remark",House.Remark), new MySqlParameter("userID",Global.UserID)});
            if (result > 0)
            {
                ShowInfo = "您的房屋编号为：" + House.ID;
                OKButtonEnable = false;
            }
        }
        private bool UpdateHouseInfo(House house)
        {
            string sql = "update house set companyName = @companyName, typeID = @typeID, seatID = @seatID, favorID = @favorID, fitmentID = @fitmentID, mothedID = @mothedID, floorID = @floorID, price = @price, buildYear = @buildYear, area = @area, remark = @remark where id = @id";
            int result = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] {
                new MySqlParameter("companyName",house.CompanyName), new MySqlParameter("typeID",house.TypeID), new MySqlParameter("seatID",house.SeatID), new MySqlParameter("favorID",house.FavorID),new MySqlParameter("fitmentID",house.FitmentID),
                    new MySqlParameter("mothedID",house.MothedID), new MySqlParameter("price",house.Price), new MySqlParameter("floorID",house.FloorID), new MySqlParameter("buildYear",house.BuildYear), new MySqlParameter("area",house.Area), new MySqlParameter("remark",house.Remark), new MySqlParameter("id",house.ID)});
            if (result > 0)
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}
